package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.feng.entity.Generate;
import org.feng.base.entity.Result;
import org.feng.service.GenerateService;

import lombok.AllArgsConstructor;
import org.feng.utils.ExcelUtils;
import org.feng.utils.condition.Condition;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 自动生成代码
 * </p>
 *
 * @author feng
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/system/generate")
@AllArgsConstructor
public class GenerateController {

    private final GenerateService generateService;


    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtils.writeExcel(response, generateService.list(), Generate.class);
    }

    /**
     * 生成代码
     */
    @GetMapping("/create")
    public Result<String> create(@RequestParam String ids) {
        generateService.create(ids);
        return Result.success();
    }

    /**
     * /**
     * 列表
     */
    @GetMapping
    public Result<List<Generate>> list(Generate generate) {
        return Result.success(generateService.list(Condition.getQueryWrapper(generate)));
    }

    /**
     * 分页列表
     */
    @GetMapping(value = "/page")
    public Result<IPage<Generate>> page(Generate generate) {
        return Result.success(generateService.page(Condition.getPage(), Condition.getQueryWrapper(generate)));
    }

    /**
     * 详情
     */
    @GetMapping("/query")
    public Result<Generate> detail(@RequestParam Long id) {
        return Result.success(generateService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody Generate generate) {
        return Result.status(generateService.save(generate));
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Generate generate) {
        return Result.status(generateService.updateById(generate));
    }

    /**
     * 删除
     */
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(generateService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

